//
//  FXHomeBannerView.m
//  FanXing
//
//  Created by defoe on 16/4/18.
//  Copyright © 2016年 kugou. All rights reserved.
//

#import "SVHomeTopicBannerView.h"
#import "UIImageView+WebCache.h"
#import "FXPageControlView.h"
//默认图片名称
#define PLACE_HOLDER_IMAGE_NAME         @"dk_pub_label_img_htbanner_730x180"
//自动轮播的时间间隔
#define AUTO_SCROLL_TIME                 3

@interface SVHomeTopicBannerView ()<FXCycleScrollViewDelegate,SDWebImageManagerDelegate> {
    
}

@property(nonatomic, strong) FXPageControlView *pageControl;
@property(nonatomic, copy) NSMutableArray *dataArray;
@property(nonatomic, strong) NSMutableArray *disDataArray;

@property (nonatomic, assign) BOOL firstReport;
@property (nonatomic, assign) BOOL secondReport;
@property (nonatomic, assign) BOOL thirdReport;

@end

@implementation SVHomeTopicBannerView

- (void)layoutSubviews{
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.bannerView = [[FXCycleScrollView alloc] initWithFrameAndDirection:self.bounds direction:FXCycleScrollViewDirectionHorizontal circularScroll:YES];
        self.bannerView.addTapGesture = YES;
        [SDWebImageManager sharedManager].delegate = self;
        WeakSelf(self);
        [self.bannerView setDataSource:^(NSUInteger index){
          
            UIView *view = [[UIView alloc] initWithFrame:weakself.bounds];
            UIImageView *reallyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, weakself.width - 10, weakself.height)];
            reallyImageView.contentMode = UIViewContentModeScaleToFill;
//            reallyImageView.image = IMAGE(PLACE_HOLDER_IMAGE_NAME);
            reallyImageView.clipsToBounds = YES;
            reallyImageView.layer.cornerRadius = 4;
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 0, weakself.width - 10, weakself.height)];
            imageView.contentMode = UIViewContentModeScaleToFill;
//            imageView.image = IMAGE(PLACE_HOLDER_IMAGE_NAME);
            imageView.clipsToBounds = YES;
            imageView.layer.cornerRadius = 4;
//            if (ArrayOutOfBounds(weakSelf.dataArray, index)) {
//                return imageView;
//            }
            
            [imageView addSubview:reallyImageView];
            id model = [weakself.dataArray objectAtIndex:index];
            BOOL showTitle = false;
            NSString *title = nil;
            NSString *imagePath;
            
            if ([model isKindOfClass:[YGBannerModel class]]) {
                showTitle = YES;
                YGBannerModel *topicModel = model;
                imagePath = topicModel.imgUrl;
                if (topicModel.name) {
                    title = topicModel.name;
                }
            }else if([model isKindOfClass:[NSString class]]){
                imagePath = model;
            }
            
            if (imagePath != nil) {
                if (imagePath != nil) {
                    [imageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//                        NSInteger imageHeight = image.size.height;
                        if (image.size.height * [UIScreen mainScreen].scale >= 320) {
                            reallyImageView.height = reallyImageView.height + 25;
                            reallyImageView.y = - 25;
                        }
                        reallyImageView.image = image;
                        
                        if (showTitle && title != nil) {
                            UIImageView *icon = [[UIImageView alloc] init];
//                            icon.image = IMAGE(@"dk_pub_topbar_icon_ht_48x48");
                            [icon sizeToFit];
                            UILabel *label = [[UILabel alloc] init];
                            label.font = [UIFont systemFontOfSize:14];
                            [label setTextColor:[UIColor whiteColor]];
                            label.text = title;
                            [label sizeToFit];
                            
                            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, icon.width + label.width + 25, 20)];
                            view.layer.cornerRadius = view.height / 2;
                            view.clipsToBounds = YES;
                            view.backgroundColor = UIColorFromRGB(0x00ddbb);
                            
                            
                            view.right = reallyImageView.width - 4;
                            view.bottom = reallyImageView.height - 4;
                            icon.bottom = view.bottom;
                            icon.left = view.left + 10;
                            
                            label.height = view.height;
                            label.left = icon.right + 5;
                            label.centerY = view.centerY;
                            [reallyImageView addSubview:view];
                            [reallyImageView addSubview:icon];
                            [reallyImageView addSubview:label];
                        }
                        
                    }];
                }
            }
            [view addSubview:imageView];
            return view;
        }];
        
        self.bannerView.controlDelegate = self;
        [self.bannerView setAutoPlay:YES];
        self.bannerView.TapActionBlock = ^(NSInteger pageIndex){
            if (weakself.homeBannerViewDelegate != nil && [weakself.homeBannerViewDelegate respondsToSelector:@selector(onBannerClick:)]) {
                [weakself.homeBannerViewDelegate onBannerClick:pageIndex];
            }
        };
        [self addSubview:self.bannerView];
        
        
        self.pageControl = [[FXPageControlView alloc] initWithFrame:CGRectMake(0, self.height - 20, 80, 20)];
        self.pageControl.numberOfPages = 1;
        self.pageControl.itemGap = 4;
        CGSize size = CGSizeMake(10, 10);
        self.pageControl.normalImage = [FXAuditUtils imageWithColor:UIColorFromRGBAlpha(0xfffff, 0.5) withSize:size];
        self.pageControl.lightImage = [FXAuditUtils imageWithColor:UIColorFromRGB(0xFFDD22) withSize:size];
        [self.pageControl setPageCorRadius];
        [self addSubview:self.pageControl];
    }
    return self;
}

- (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL
{
    return image;
}

#pragma mark
#pragma mark PublicMehtod
- (void)updateContentInfo:(NSArray <YGBannerModel *> *)bannerArray {
    if (bannerArray == nil || [bannerArray count] == 0) {
        return;
    }
    
    self.dataArray = bannerArray;
    self.disDataArray = [NSMutableArray array];
    //更新banner的视图
    NSUInteger bannerCount = [self.dataArray count];
    self.bannerView.numberOfPages = bannerCount;
    self.pageControl.numberOfPages = bannerCount;
    self.pageControl.currentPage = self.bannerView.currentPage;
    
    if (bannerCount <= 1) {
        self.pageControl.hidden = YES;
    }else{
        self.pageControl.hidden = NO;
    }
}

#pragma mark- PrivateMethod
-(UIImage*)handleImageToScale1:(UIImage*)image
{
    UIImage *  scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:1 orientation:image.imageOrientation];
    
    UIGraphicsBeginImageContext(CGSizeMake(scaledImage.size.width,scaledImage.size.height));
    
    int offsetY = 0;
    if(scaledImage.size.height<320)
        offsetY = 0;
    else
        offsetY = -30;
    
    //250高度的时候不往上移动
    [scaledImage drawInRect:CGRectMake(0,offsetY,scaledImage.size.width, scaledImage.size.height)];
    
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage;
}

#pragma mark
#pragma mark FXCycleScrollViewDelegate
- (void)lazyScrollView:(FXCycleScrollView *)pagingView currentPageChanged:(NSInteger)currentPageIndex {
     self.pageControl.currentPage = currentPageIndex;
    BOOL exit = NO;
    for (NSNumber *obj in self.disDataArray) {
        NSInteger value = [obj integerValue];
        if (value == currentPageIndex) {
            exit = YES;
        }
    }
//    if (!exit) {
//        if (self.disDataArray.count == 0) {
//            [self.disDataArray addObjectsFromArray:self.dataArray];
//        }
//        [self.disDataArray objectAtIndex:@(currentPageIndex)];
//        YGBannerModel *model = [self.dataArray objectAtIndex:currentPageIndex];
//    }
    
}

@end
